package com.epam.iunna.bookshop.controller.actions;


import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.iunna.bookshop.controller.application.Constants;
import com.epam.iunna.bookshop.controller.utils.dispatch.ActionInt;
import com.epam.iunna.bookshop.model.service.BasketBean;

public class DelItemFromBasket implements ActionInt{

    @Override
    public String perform(HttpServletRequest request,
            HttpServletResponse response) {
        Long itemID = Long.valueOf(0);
        try{
            itemID = Long.parseLong(request.getParameter("itemID"));
            
            BasketBean basketBean = (BasketBean) request.getSession().getAttribute(BasketBean.BASKET_BEAN_SESSION_BEAN);
            if( basketBean == null ){
                System.out.println("Basket is not avaliable in session for deleting item with id =" + itemID);
                return Constants.FATAL_ERROR_PAGE_PATH;
            }
            
            basketBean.removeItemFromBasket( itemID );
            
        }catch(NumberFormatException e){
            System.out.println("Incorrect item id " + itemID );
            e.printStackTrace();
            return Constants.FATAL_ERROR_PAGE_PATH;
        } catch (SQLException e) {
            System.out.println("Cannot remove item from basket item id " + itemID );
            e.printStackTrace();
        }
        BooksList bookListAction = new BooksList();
        return bookListAction.perform(request, response);
    }

}
